package com.acme.dao.impl;

import java.util.ArrayList;
import java.util.List;





import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.acme.dao.ZipcodeDAO;
import com.acme.entity.Zipcode;

@Service
public class ZipcodeDAOImpl implements ZipcodeDAO {
	
	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public List<Zipcode> getAllZipcode() {
		Query query = sessionFactory.getCurrentSession().createQuery("from Zipcode");
		List<?> l = query.list();
		ArrayList<Zipcode> zipCodeList = new ArrayList<Zipcode>();
		for (Object o : l) {
			zipCodeList.add((Zipcode) o);
		}
		
		return zipCodeList;
	}

	@Override
	public Zipcode getZipcodeByID(int id) {
		return (Zipcode) sessionFactory.getCurrentSession().get(Zipcode.class, id);
	}

	
	public List<Zipcode> getZipcodeByZip(int zipcode) {
		String queryString = "from Zipcode where zipcode = " + zipcode;
		Query query = sessionFactory.getCurrentSession().createQuery(queryString);
		ArrayList<Zipcode> zipCodeList = new ArrayList<Zipcode>();
		List<?> l = query.list();
		for (Object o : l) {
			zipCodeList.add((Zipcode) o);
		}
		
		return zipCodeList;
	}

}
